
#include "client.h"


int joint_stdin(struct login_status* stat,struct client_info* info)
{

    switch(info->command)
    {
    case LOGIN:

        info->num=2;
        sprintf(info->protocol,"name=%s&password=%s",stat->name,stat->password);
        break;
    case REGIST:
        info->num=2;
        sprintf(info->protocol,"name=%s&password=%s",stat->name,stat->password);
        break;
    default:
        info->num=3;
        char param[100]={0};
        strcpy(param,info->protocol);
        sprintf(info->protocol,"name=%s&path=%s&param=%s",stat->name,
                stat->path,
                param);
        break;
    }
    return 0;
}
int ensure_command(const char* command,struct client_info* info)
{
    int length = strlen(command);

    char* commandarr[8]={"rm","ls","mkdir","puts","gets","pwd","cd","exit"};

    for(int i=0;i<8;i++)
    {
        int len=strlen(commandarr[i]); 

        if(len==length&&!strcmp(command,commandarr[i]))
        {
            info->command=(enum command)i;
            return 0;
        }
    }
    info->command=ERR;
    return 0;
}
int getparama(const struct client_info* info,char* msg)
{
    int num =info->num;

    char buf[100]={0};

    strcpy(buf,info->protocol);
    printf("buf:protocol:%s\n",buf);
    char* arr[6]={0};
    switch(info->command)
    {
    case GETS: case PUTS:
        arr[0]=strtok(buf,"&");
        printf("arr[0]:%s\n",arr[0]);
        for(int i=1;i<num;i++)
        {
            arr[i]=strtok(NULL,"&");
            printf("arr[%d]:%s\n",i,arr[i]);
        }
        char *p;
        printf("arr[num-1]:%s\n",arr[num-1]);
        p=strtok(arr[num-1],"=");
        p=strtok(NULL,"=");
        printf("P:%s\n",p);
        char buffer[100];


        while(p!=NULL)
        {
            memset(buffer,0,sizeof(buffer));
            printf("P:%s\n",p);
            strcpy(buffer,p);
            p=strtok(NULL,"/");
        }
        //memcpy(msg,buffer,strlen(buffer));
        strcpy(msg,buffer);
        printf("msg:%s\n",msg);
        break;
    default:
        break;
    }
    return 0;
}





